package com.factory.iamp.utils.helper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.pojo.TableContent;
import com.factory.iamp.pojo.TableTemplateBody;
import com.factory.iamp.utils.exception.APIException;

import java.util.ArrayList;
import java.util.List;

public class CheckTableHelper {
    public static void ValidCheck(
            CheckTypeCode typeCode, JSONObject contentValue,
            TableContent tableContent, TableTemplateBody bodyFormat
    ) {
        if(typeCode == CheckTypeCode.SelfCheck) {
            List<String> needIndex = JSONArray.parseArray(bodyFormat.getFormat()).toJavaList(String.class);
            List<String> filledIndex = new ArrayList<>(contentValue.keySet());
            filledIndex.add("Ocheck");
            filledIndex.add("Scheck");
            filledIndex.add("Zcheck");
            filledIndex.add("Problem");
//            if(!(needIndex.containsAll(filledIndex) && filledIndex.containsAll(needIndex)))
            if(!filledIndex.containsAll(needIndex))
                throw new APIException(ResponseCode.DATABASE_FAILED, "表单项未填写完整，无法自检");
        }
        if(typeCode == CheckTypeCode.OtherCheck) {
            if(tableContent.getScheck().equals("need")) {
                throw new APIException(ResponseCode.DATABASE_FAILED, "未进行自检，无法互检");
            }
        }
        if(typeCode == CheckTypeCode.ZCheck) {
            if(tableContent.getOcheck().equals("need")) {
                throw new APIException(ResponseCode.DATABASE_FAILED, "未进行互检，无法Z检");
            }
        }
    }
}
